import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Direction, Images


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-WLAN，开启WLAN')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(1)
        device.dirc_fling(Direction.Up)
        device.click(*self.app.wlan_item)
        device.wait(3)
        device.refresh_layout()
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
        logging.info('预期结果1.WLAN按钮被点亮，展示可用WLAN列表')
        device.wait(20)
        switch_area = device.get_element_area(toggle)
        switch_pic = device.save_snapshot_to_local('wifi_switch.jpeg')
        Images.crop_picture(switch_pic, *switch_area)
        similarity = Images.compare_image_similarity(switch_pic, self.app.switch_on)
        assert similarity > 0.8, 'wifi开关未点亮'
        device.refresh_layout()
        device.assert_text_exist('可用WLAN')
        device.assert_text_exist('加密')
        device.assert_text_exist(self.app.encrypt_wifi)

        logging.info('步骤2.选择加密wifi，点击进入，点击密码后方显示/隐藏按钮，输入加密wifi的密码')
        target_wifi = device.get_element_by_text(self.app.encrypt_wifi)
        device.click_element(target_wifi)
        device.refresh_layout()
        pwd_area = device.get_element_by_type('TextInput')
        eye_icon = device.get_element_by_type('SymbolGlyph')
        px, py = device.center_of_element(pwd_area)
        device.input_text(px, py, self.app.wifi_pwd)
        device.refresh_layout()
        pwd1 = device.get_element_by_type('TextInput').get('text')
        device.click_element(eye_icon)
        device.refresh_layout()
        pwd2 = device.get_element_by_type('TextInput').get('text')
        logging.info('预期结果2.选择显示则可以看到输入的密码；选择隐藏则输入的密码以“.”符号代替显示')

        assert pwd1 == '********', '密码未以“.”号形式显示'
        assert pwd2 == self.app.wifi_pwd, '密码未明文显示'
